package demo1;

public class Sort {
    //1.直接插入排序
    public static void insertSort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int temp = array[i];
            int j = i -1;
            for (;j >= 0;j--) {
                if (array[j] > temp) {
                    array[j+1] = array[j];
                } else {
                    break;
                }
            }
            array[j+1] = temp;
        }
    }
    //2.希尔排序
    public static void shellSort(int[] array) {
        int gap = array.length;
        while (gap >= 1) {
            gap /= 2;
            shell(array,gap);
        }
    }
    //对每一组使用直接插入排序
    public static void shell(int[] array,int gap) {
        for (int i = gap; i < array.length; i++) {
            int temp = array[i];
            int j = i - gap;
            for (; j >= 0; j -= gap) {
                if (temp < array[j]) {
                    array[j+gap] = array[j];
                } else {
                    break;
                }
            }
            array[j+gap] = temp;
        }
    }
    //3.直接选择排序
    public static void selectSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            for (int j = i + 1; j < array.length; j++) {
                if (array[j] < array[minIndex]) {
                    minIndex = j;
                }
            }
            swap(array,minIndex,i);
        }
    }
    //4.冒泡排序
    public static void bubbleSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            boolean flag = false;
            for (int j = 0; j < array.length - i - 1; j++) {
                if (array[j] > array[j + 1]) {
                    swap(array,j,j+1);
                    flag = true;
                }
            }
            if (flag == false) {
                break;
            }
        }
    }
    //5.堆排序
    public static void heapSort(int[] array) {
        creatBigHeap(array);
        int end = array.length-1;
        while (end > 0) {
            swap(array,0,end);
            shiftDown(0,array,end);
            end--;
        }
    }
    //创建大根堆
    private static void creatBigHeap(int[] array) {
        for (int i = (array.length-1-1)/2; i >= 0; i--) {
            shiftDown(i,array,array.length);
        }
    }
    //向下调整
    private static void shiftDown(int parent, int[] array, int end) {
        int child = parent*2+1;
        while (child < end) {
            if(child + 1 < end && array[child] < array[child+1]) {
                child++;
            }
            if(array[child] > array[parent]) {
                swap(array,child,parent);
                parent = child;
                child = parent*2+1;
            } else {
                break;
            }
        }
    }
    //6.快速排序
    public static void quickSort(int[] array) {
        quick(array,0,array.length-1);
    }

    private static void quick(int[] array, int begin, int end) {
        if (begin >= end) {
            return;
        }
        int left = begin;
        int right = end;
        int pivot = parttion(array,left,right);
        quick(array,left,pivot-1);
        quick(array,pivot+1,end);
    }

    private static int parttion(int[] array, int left, int right) {
        //int pivot = array[left];
        findMid(array,left,right);
        int pivot = array[left];
        while (left < right) {
            while (array[right] > pivot && left < right) {
                right--;
            }
            array[left] = array[right];
            while (array[left] < pivot && left < right) {
                left++;
            }
            array[right] = array[left];
        }
        array[left] = pivot;
        return left;
    }

    private static void findMid(int[] array, int left, int right) {
        int mid = (left + right)/2;
        if (array[left] > array[right]) {
            if(array[right] > array[mid]) {
                swap(array,right,left);
                return;
            } else {
                if(array[mid] > array[left]) {
                    return;
                } else {
                    swap(array,left,mid);
                    return ;
                }
            }
        } else {
            if(array[mid] > array[right]) {
                swap(array,right,left);
                return;
            } else {
                if(array[mid] > array[left]) {
                    swap(array,left,mid);
                    return;
                } else {
                    return;
                }
            }
        }
    }

    private static void swap(int[] array, int i, int j) {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
    //7. 归并排序
    public static void mergeSort(int[] array) {
        mergeSortFuc(array,0,array.length-1);
    }
    private static void mergeSortFuc(int[] array, int left, int right) {
        if(left >= right) {
            return;
        }
        int mid = (left + right) / 2;
        mergeSortFuc(array,left,mid);
        mergeSortFuc(array,mid+1,right);
        merge(array,left,mid,right);
    }
    private static void merge(int[] array, int left, int mid, int right) {
        int s1 = left;
        int s2 = mid + 1;
        int e1 = mid;
        int e2 = right;
        int[] temp = new int[right - left +1];
        int k = 0;
        while(s1 <= mid && s2 <= right) {
            if(array[s1] <= array[s2]) {
                temp[k++] = array[s1++];
            } else {
                temp[k++] = array[s2++];
            }
        }
        while(s1 <= mid) {
            temp[k++] = array[s1++];
        }
        while(s2 <= right) {
            temp[k++] = array[s2++];
        }
        for(int i = 0; i < k; i++) {
            array[left+i] = temp[i];
        }
    }


    public static void main(String[] args) {
        int[] array = { 43, 51, 36, 67, 18, 29 };
        insertSort(array);
        int[] array2 = { 43, 51, 36, 67, 18, 29 };
        shellSort(array2);
        int[] array3 = { 43, 51, 36, 67, 18, 29 };
        selectSort(array3);
        int[] array4 = { 43, 51, 36, 67, 18, 29 };
        bubbleSort(array4);
        int[] array5 = { 43, 51, 36, 67, 18, 29 };
        heapSort(array5);
        int[] array6 = { 43, 51, 36, 67, 18, 29 };
        quickSort(array6);
        int[] array7 = { 43, 51, 36, 67, 18, 29 };
        mergeSort(array7);
        System.out.println();
    }
}
